<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateTableUserWalletAndDetails extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //用户交易记录表
        Schema::create('transitions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('number')->default(0)->nullable(false)->comment('交易数量');
            $table->unsignedTinyInteger('transtype')->default(0)->nullable(false)
                  ->comment('交易类型.1为预定订单订金付款、2为预定订单结账付款、3为商品订单付款、4为充值、5为反馈转钱包、6为反馈、7为提现、8佣金');
            $table->unsignedTinyInteger('paytype')->default(0)->nullable(false)
                ->comment('支付方式.1为钱包支付、2为反馈钱包支付、3为现金支付、4提现');
            $table->string('transname')->default('')->nullable(false)->comment('交易名称');
            $table->bigInteger('orderid')->default(0)->nullable(false)->comment('关联的订单ID');
            $table->string('transno')->unique()->default('')->nullable(false)->comment('交易单号');
            $table->unsignedInteger('balance')->default(0)->nullable(false)->comment('交易后钱包余额');
            $table->unsignedInteger('cycle_balance')->default(0)->nullable(false)->comment('交易后反馈钱包余额');
            $table->tinyInteger('status')->default(1)->nullable(false)->comment('交易状态 1待交易 2成功 3退订 4取消');
            $table->unsignedInteger('uid')->default(0)->nullable(false)->comment('关联的用户ID');
            $table->foreign('uid')->references('id')->on('users');
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
        DB::statement("alter table `transitions` comment '用户交易记录表' ");

        //用户积分明细表
        Schema::create('jifen_details', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('number')->default(0)->nullable(false)->comment('交易积分数量');
            $table->unsignedTinyInteger('transtype')->default(0)->nullable(false)
                ->comment('交易类型.1为积分兑换、2为反馈积分');
            $table->string('transno')->unique()->default('')->nullable(false)->comment('交易单号');
            $table->string('transname')->default('')->nullable(false)->comment('交易名称');
            $table->unsignedInteger('coin')->default(0)->nullable(false)->comment('交易后剩余积分');
            $table->unsignedInteger('uid')->default(0)->nullable(false)->comment('关联的用户ID');
            $table->bigInteger('orderid')->default(0)->nullable(false)->comment('关联的订单ID');
            $table->tinyInteger('status')->default(1)->nullable(false)->comment('交易状态 1待交易 2成功 3取消');
            $table->foreign('uid')->references('id')->on('users');
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
        DB::statement("alter table `jifen_details` comment '用户积分明细表' ");

        //用户充值记录表
        Schema::create('recharges', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('number')->default(0)->nullable(false)->comment('充值金额，单位为分');
            $table->unsignedTinyInteger('transtatus')->default(0)->nullable(false)->comment('交易状态.0审核中、1审核成功、2审核失败');
            $table->string('evidence')->default('')->nullable(false)->comment('充值凭证');
            $table->string('toname')->default('')->nullable(false)->comment('付款对象');
            $table->string('paybank')->default('')->nullable(false)->comment('收款开户行');
            $table->string('bankcard')->default('')->nullable(false)->comment('收款银行卡号');
            $table->unsignedInteger('uid')->default(0)->nullable(false)->comment('关联的用户ID');
            $table->foreign('uid')->references('id')->on('users');
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
        DB::statement("alter table `recharges` comment '用户充值记录表' ");

        //钱包银行卡表
        Schema::create('bankcards', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('cardowner')->default('')->nullable(false)->comment('卡主姓名');
            $table->string('bankname')->default('')->nullable(false)->comment('归属银行名称');
            $table->string('bankcard')->default('')->nullable(false)->comment('银行卡号');
            $table->unsignedInteger('uid')->default(0)->nullable(false)->comment('关联的用户ID');
            $table->foreign('uid')->references('id')->on('users');
            $table->timestamps();
        });
        DB::statement("alter table `bankcards` comment '钱包银行卡表' ");

        //用户提现记录表
        Schema::create('withdraws', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('number')->default(0)->nullable(false)->comment('提现金额，单位为分');
            $table->unsignedTinyInteger('transtatus')->default(0)->nullable(false)->comment('提现状态.0申请中、1提现通过、2提现拒绝');
            $table->string('evidence')->default('')->nullable(false)->comment('提现凭证');
            $table->string('remark')->default('')->nullable(false)->comment('拒绝理由');
            $table->string('cardowner')->default('')->nullable(false)->comment('卡主姓名');
            $table->string('bankname')->default('')->nullable(false)->comment('归属银行名称');
            $table->string('bankcard')->default('')->nullable(false)->comment('银行卡号');
            $table->unsignedInteger('uid')->default(0)->nullable(false)->comment('关联的用户ID');
            $table->foreign('uid')->references('id')->on('users');
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
        DB::statement("alter table `withdraws` comment '用户提现记录表' ");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('transitions');
        Schema::dropIfExists('jifen_details');
        Schema::dropIfExists('recharges');
        Schema::dropIfExists('bankcards');
        Schema::dropIfExists('withdraws');
    }
}
